Passed
Push — master ( cc7767...b0feda )
by Stefan
11:56
created

app.Drag   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
rs 9.9
c 2
b 0
f 0
1
/* 
2
 * this code based on https://openlayers.org/en/latest/examples/custom-interactions.html
3
 */
4
5
/** global: ol */
6
/** global: tmpLayer */
7
8
window.app = {};
9
var app = window.app;
10
app.Drag = function () {
11
    ol.interaction.Pointer.call(this, {
12
        handleDownEvent: handleDownEvent,
13
        handleDragEvent: handleDragEvent,
14
        handleMoveEvent: handleMoveEvent,
15
        handleUpEvent: handleUpEvent
16
    });
17
18
    this.coordinate_ = null;
19
    this.cursor_ = 'pointer';
20
    this.feature_ = null;
21
    this.previousCursor_ = undefined;
22
};
23
24
ol.inherits(app.Drag, ol.interaction.Pointer);
25
26
function handleDownEvent(evt) {
27
    var map = evt.map;
28
    var feature = map.forEachFeatureAtPixel(evt.pixel,
29
            basicFeatureHandler,
30
            {layerFilter: layerFilter}
31
    );
32
    if (feature) {
33
        this.coordinate_ = evt.coordinate;
34
        this.feature_ = feature;
35
    }
36
    return !!feature;
37
}
38
39
function handleDragEvent(evt) {
40
    var map = evt.map;
41
    var feature = map.forEachFeatureAtPixel(evt.pixel,
0 ignored issues
show
Unused Code introduced by
The variable feature seems to be never used. Consider removing it.
Loading history...
42
            basicFeatureHandler,
43
            {layerFilter: layerFilter});
44
45
    var deltaX = evt.coordinate[0] - this.coordinate_[0];
46
    var deltaY = evt.coordinate[1] - this.coordinate_[1];
47
48
    var geometry = /** @type {ol.geom.SimpleGeometry} */
49
            (this.feature_.getGeometry());
50
    geometry.translate(deltaX, deltaY);
51
52
    this.coordinate_[0] = evt.coordinate[0];
53
    this.coordinate_[1] = evt.coordinate[1];
54
}
55
56
function handleMoveEvent(evt) {
57
    if (this.cursor_) {
58
        var map = evt.map;
59
        var feature = map.forEachFeatureAtPixel(evt.pixel,
60
                basicFeatureHandler,
61
                {layerFilter: layerFilter});
62
        var element = evt.map.getTargetElement();
63
        if (feature) {
64
            if (element.style.cursor != this.cursor_) {
65
                this.previousCursor_ = element.style.cursor;
66
                element.style.cursor = this.cursor_;
67
            }
68
        } else if (this.previousCursor_ !== undefined) {
69
            element.style.cursor = this.previousCursor_;
70
            this.previousCursor_ = undefined;
71
        }
72
    }
73
}
74
75
function handleUpEvent(evt) {
76
    setTmpPointer(evt.coordinate);
77
    this.coordinate_ = null;
78
    this.feature_ = null;
79
    return false;
80
}
81
82
function layerFilter(layer) {
83
    if (layer === tmpLayer)
84
        return(true);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
85
    else
86
        return(false);
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
87
}
88
89
function basicFeatureHandler(feature, layer) {
0 ignored issues
show
Unused Code introduced by
The parameter layer is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
90
    return(feature);
91
}
92
93
94